package com.zhangxi;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class MyPanel extends JPanel implements KeyListener, ActionListener {
    //申明右侧蛇头和身体图片
    ImageIcon right = new ImageIcon("images/right.png");
    ImageIcon body = new ImageIcon("images/body.png");

    //申明上、下、左侧的蛇头图片
    ImageIcon top = new ImageIcon("images/top.png");
    ImageIcon bottom = new ImageIcon("images/bottom.png");
    ImageIcon left = new ImageIcon("images/left.png");


    //申明一个初始值，表示蛇的长度为3
    int len = 3;
    //申明两个数组分别存放蛇的x和y的坐标位置
    int[] snakeX = new int[1008]; //最大值 = 宽度格子 * 高度格子
    int[] snakeY = new int[1008]; //最大值 = 宽度格子 * 高度格子

    //申明枚举类型变量，标识蛇头方向
    Direction direction = Direction.bottom;

    //申明一个游戏变量，isStart标记游戏的状态，当为false时，表示游戏开始，为true时表示游戏开始
    boolean isStart = false;

    //创建一个定时器对象
    Timer timer = new Timer(100, this);
    //申明两个变量表示食物的位置
    int foodX;
    int foodY;
    //申明一个随机数
    Random  random = new Random();
    //申明食物图片
    ImageIcon food = new ImageIcon("images/food.png");
    public MyPanel() {
        //设置蛇的头部和身体的初始值
        snakeX[0] = 100;
        snakeY[0] = 100;

        snakeX[1] = 75;
        snakeY[1] = 100;

        snakeX[2] = 50;
        snakeY[2] = 100;
        //设置获取焦点为true
        this.setFocusable(true);
        //添加监听
        this.addKeyListener(this);
        //启动定时器
        timer.start();
        //生成食物foodX与foodY坐标位置
        foodX = 25 + 25 * random.nextInt(20);
        foodY = 25 + 25 * random.nextInt(20);


    }

    @Override
    protected void paintComponent(Graphics g) {
        //调用父类值的方法做一些基本工作
        super.paintComponent(g);
        //设置背景颜色
        this.setBackground(Color.red);
        //在画布中添加游戏区域
        g.fillRect(0, 0, 700, 900);
        //添加头部图片
//        right.paintIcon(this, g, snakeX[0], snakeY[0]);
        // 通过枚举值控制蛇头方向
        switch (direction) {
            case top:
                top.paintIcon(this, g, snakeX[0], snakeY[0]);
                break;
            case bottom:
                bottom.paintIcon(this, g, snakeX[0], snakeY[0]);
                break;
            case left:
                left.paintIcon(this, g, snakeX[0], snakeY[0]);
                break;
            case right:
                right.paintIcon(this, g, snakeX[0], snakeY[0]);
                break;
        }

        //添加两个身体
        /*  body.paintIcon(this,g,75,100);
        body.paintIcon(this,g,50,100);*/
        for (int i = 1; i < len; i++) {
            body.paintIcon(this, g, snakeX[i], snakeY[i]);
        }

        if (!isStart) {
            // 放上开始游戏提醒，并设置字体颜色和字体
            g.setColor(Color.white);
            g.setFont(new Font("宋体", Font.BOLD, 50));
            g.drawString("请按空格键表示游戏开始", 50, 500);

        }

        //添加食物
        food.paintIcon(this,g,foodX,foodY);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == 32) { //判断当前空格值为32
            //游戏状态值取反
            isStart = !isStart;
            //重新画组件
            repaint();
        } else if (keyCode == KeyEvent.VK_UP) {
            direction = Direction.top;
        } else if (keyCode == KeyEvent.VK_DOWN) {
            direction = Direction.bottom;
        } else if (keyCode == KeyEvent.VK_LEFT) {
            direction = Direction.left;
        } else if (keyCode == KeyEvent.VK_RIGHT) {
            direction = Direction.right;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (isStart) {
            //移动身体
            for (int i = len - 1; i > 0; i--) {
                snakeX[i] = snakeX[i - 1];
                snakeY[i] = snakeY[i - 1];
            }
            switch (direction) {
                case top:
                    snakeY[0] -= 25;
                    if (snakeY[0] <= 0) {
                        snakeY[0] = 900 ;
                    }
                    break;
                case bottom:
                    snakeY[0] += 25;
                    if (snakeY[0] >=900 ) {
                        snakeY[0] = 0 ;
                    }
                    break;
                case left:
                    snakeX[0] -= 25;
                    if (snakeX[0] <= 0) {
                        snakeX[0] = 700 ;
                    }
                    break;
                case right:
                    snakeX[0] += 25;
                    if (snakeX[0] >= 700) {
                        snakeX[0] = 0 ;
                    }
                    break;

            }
//            // 判断，当蛇头值超出700，则x值从0开始
//            if (snakeX[0] >= 700) {
//                snakeX[0] = 0;
//            }

            //判断，当蛇头x和食物x坐标一致时，并且蛇头y与食物y坐标一致，则表示吃到食物
            if(snakeX[0] == foodX && snakeY[0] == foodY){
                len ++;
                foodX = 25 + 25 * random.nextInt(20);
                foodY = 25 + 25 * random.nextInt(20);

            }
            //重新画组件
            repaint();
            //重新启动定时器
            timer.start();
        }
    }
}
